package org.example.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @description: 消息生产者
 */
public class FanoutProducerTest {

    //交换机名称
    public static final String EXCHANGE_FANOUT = "exchange_fanout_test";

    /**
     * 步骤：
     * 1、创建连接
     * 2、创建信道
     * 3、创建交换机
     * 4、发送消息
     */
    public static void main(String[] args) throws Exception {
        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置连接的mq服务器的IP地址
        factory.setHost("localhost");
        //1、创建连接
        try(Connection connection = factory.newConnection()){
            //2、创建信道
            Channel channel = connection.createChannel();
            //3、创建交换机
            channel.exchangeDeclare(EXCHANGE_FANOUT, BuiltinExchangeType.FANOUT);

            //4、发送消息
            for (int i = 1; i <= 5; i++) {
                channel.basicPublish(EXCHANGE_FANOUT, "", null, ("这是一个fanout消息" + i).getBytes());
            }
            System.out.println("消息发送完毕");
        }
    }
}